//
//  PasswordChangeViewController.m
//  Comma
//
//  Created by 杜远洋 on 15-1-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PasswordChangeViewController.h"
#import "PasswordChangeView.h"
#import "AFNetWorkingYU.h"
#import "URLMacro.h"
#import "GlobalMethod.h"
#import "YRSideViewController.h"
@interface PasswordChangeViewController ()

@end

@implementation PasswordChangeViewController
- (void)loadView
{
    [super loadView];
    // 不能划出侧边栏
    YRSideViewController *slide = [YRSideViewController share];
    slide.needSwipeShowMenu = NO;
    
    self.password = [[PasswordChangeView alloc] initWithLeftButton:@"返回" RightButton:@"" Title:@"密码修改" FooterView:FooterViewStateNone];
    self.view = self.password;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDataFromServer];
    self.password.passwordDic = [NSMutableDictionary dictionary];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.password.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.password.confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)leftButtonAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 从服务器获取数据
- (void)getDataFromServer
{
    NSString *key = [GlobalMethod getApproveKey];
    NSString *device = [GlobalMethod GetIOSUUID];
    
    NSString *str =[NSString stringWithFormat:@"approveKey=%@&deviceParam=%@", device, key];    NSLog(@"%@",str);
    NSString *myUrl = [NSString stringWithFormat:@"%@?%@",URL_USER_GETDETAIL , str];
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:myUrl resultBlock:^(id result) {
        if ([result isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = (NSDictionary *)result;
        NSString *my = [dic objectForKey:@"msg"];
        my = [my stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@",my);
        if ([[dic objectForKey:@"msg"] isEqualToString:@"success"])
        {
            
#pragma mark -   服务器返回数据 从数据里得到oldpassword，看看是否和输入的一致 在viewcontroller里判断
            
            NSDictionary *dic = (NSDictionary *)result;
            
            NSDictionary *myDic = [dic objectForKey:@"obj"];
            //密码是加密的md5格式的。。。
            
            [self.password.passwordDic setValue:[myDic objectForKey:@"password"] forKey:@"password"];
            
            
            
        }
        } else {
#warning <<<
        }
        
    }];
    
    
}

#pragma mark - 传给服务器数据(注释：总出现未知错误 ， 新密码传不上去)
- (void)transferMyDataToServer
{
    NSString *key = [GlobalMethod getApproveKey];
    NSString *device = [GlobalMethod GetIOSUUID];
    
    NSString *str =[NSString stringWithFormat:@"approveKey=%@&deviceParam=%@&password=%@", device, key, self.password.newsPassword.text];
    NSLog(@"%@",str);
    //    NSString *myUrl = [NSString stringWithFormat:@"%@?%@",URL_USER_INFO_EDIT , str];
    //    NSLog(@"%@",myUrl);
    
    [AFNetWorkingYU netWorkWithURL:@"http://www.isu.so/user/edit_pwd" body:str resultBlock:^(id result) {
        if ([result isKindOfClass:[NSDictionary class]]) {
        NSString *str = [result objectForKey:@"msg"];
        str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"返回消息：%@", str);
        } else {
#warning <<<
        }
        
    }];
}

#pragma mark - 点击确定按钮时，验证密码是否正确  新密码输入是否一致
- (void)confirmButtonAction:(id)sender
{
    
#pragma mark - 系统版本的判断
    NSString *myPassword = [GlobalMethod md5:self.password.oldPassword.text];
    myPassword = [myPassword lowercaseString];
    NSLog(@"%@", myPassword);
    if ([myPassword isEqualToString:[self.password.passwordDic objectForKey:@"password"]] && [self.password.newsPassword.text isEqualToString:self.password.repeatPassword.text]) {
        if ([self.password.newsPassword.text isEqualToString:@""] || [self.password.newsPassword.text isEqualToString:@""]) {
            if (IOS_VERSION >= 8.0) {
                UIAlertController *alertConfirm = [UIAlertController alertControllerWithTitle:@"抱歉" message:@"新密码输入不能为空" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertConfirm animated:YES completion:nil];
                [self performSelector:@selector(dismissMyAlertView) withObject:alertConfirm afterDelay:1.5];
            }
            else
            {
                UIAlertView *alertConfirm = [[UIAlertView alloc] initWithTitle:@"抱歉" message:@"新密码输入不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alertConfirm show];
                [self performSelector:@selector(dismissAlert:) withObject:alertConfirm afterDelay:1.5];
            }
            
        }
        else
        {
            if (IOS_VERSION >= 8.0) {
                UIAlertController *alertConfirm = [UIAlertController alertControllerWithTitle:@"恭喜" message:@"密码修改成功" preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:alertConfirm animated:YES completion:nil];
                [self performSelector:@selector(dismissMyAlertView) withObject:alertConfirm afterDelay:1.5];
            }
            else
            {
                UIAlertView *alertConfirm = [[UIAlertView alloc] initWithTitle:@"恭喜" message:@"密码修改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alertConfirm show];
                [self performSelector:@selector(dismissAlert:) withObject:alertConfirm afterDelay:1.5];
            }
            [self transferMyDataToServer];
        }
        
        
#pragma mark - 在点击确定按钮之后 向服务器发送数据（成功后）
        
        
    }
    else if (![self.password.newsPassword.text isEqualToString:self.password.repeatPassword.text])
    {
        if (IOS_VERSION >= 8.0) {
            
            UIAlertController *differentPassword = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入密码不一致, 请重新输入" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:differentPassword animated:YES completion:nil];
            [self performSelector:@selector(dismissMyAlertView) withObject:differentPassword afterDelay:1.2];
            
        }
        else
        {
            UIAlertView *differentPassword = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入密码不一致, 请重新输入" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [differentPassword show];
            [self performSelector:@selector(dismissAlert:) withObject:differentPassword afterDelay:1.2];
            
        }
        
        
    }
    else
    {
        if (IOS_VERSION >= 8.0) {
            
            UIAlertController *wrongPassword = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不正确, 请确认后输入" preferredStyle:UIAlertControllerStyleAlert];
            NSLog(@"%@",wrongPassword.view.subviews);
            [self presentViewController:wrongPassword animated:YES completion:nil];
            [self performSelector:@selector(dismissMyAlertView) withObject:wrongPassword afterDelay:1.2];
            
        }
        else
        {
            UIAlertView *wrongPassword = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码不正确, 请确认后输入" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [wrongPassword show];
            [self performSelector:@selector(dismissAlert:) withObject:wrongPassword afterDelay:1.2];
        }
        
    }
    
}

#pragma mark - 解除视图alert的显示
- (void)dismissMyAlertView
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

- (void)dismissAlert:(UIAlertView *)alert
{
    if(alert){
        [alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
        
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
